package com.daoyin.ssph.action;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

import com.daoyin.ssph.bean.Active;
import com.daoyin.ssph.bean.Caselist;
import com.daoyin.ssph.service.CaselistService;

public class CaselistAction {
	private CaselistService ca;

	public void setCa(CaselistService ca) {
		this.ca = ca;
	}
	private String id;
	private String title;
	private String content;
	private String time;
	public void setId(String id) {
		this.id = id;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setContent(String content) {
		this.content = content;
	}
	public void setTime(String time) {
		this.time = time;
	}
	
	private File img;
	public File getImg() {
		return img;
	}
	public void setImg(File img) {
		this.img = img;
	}
	private String imgFileName;
	

	public String getImgFileName() {
		return imgFileName;
	}

	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;
	}
	
	public String upload()throws IOException{
		if(img == null){
			return "fail";
		}
		InputStream is = new FileInputStream(img);
		String root = ServletActionContext.getServletContext().getRealPath("common/upload");
		
		DateFormat format=new SimpleDateFormat("yyMMddHHmmss");
        String formatDate=format.format(new Date());
        int random=new Random().nextInt(100000);//鐢熸垚涓�釜闅忔満鏁�
        int position=imgFileName.lastIndexOf(".");//鑾峰彇鏂囦欢鍚庣紑鍚�鍦ㄧ伀鐙愭寚鍑鸿繖琛屾湁閿欍�
        String extension=imgFileName.substring(position);
        String newFileName=formatDate+random+extension;
		File distFile = new File(root,newFileName);
		OutputStream os = new FileOutputStream(distFile);
		byte[] buffer = new byte[400];
		int length = 0;
		while((length = is.read(buffer)) > 0){
			os.write(buffer,0,length);
		}

		is.close();
		os.close();

		//鏁版嵁搴撳瓨鏀句互涓嬭矾寰勶紝褰撻渶瑕佸湪椤甸潰鏄剧ず锛岀洿鎺ユ彁鍙栧嚭鏉ョ敤IMG鏍囩瑁呰浇鍗冲彲
		String imgurl ="./common/upload/" + distFile.getName();
		return imgurl;
	}
	
	public String add() throws IOException{
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		Caselist t = new Caselist();
		t.setTitle(title);
		t.setContent(content);
		t.setTime(time);
		t.setPublish(0);
		String imgurl = upload();
		
		if(imgurl.equals("fail")){
			return "fail";
		}
		
		t.setImg(imgurl);
		String str = ca.add(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}

	public String show(){
		HttpServletRequest request = ServletActionContext.getRequest();

		List<Caselist> list = ca.query();
		
		request.setAttribute("show", list);
		return "success";
	
	}
	public String showDetails(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Caselist caselist = ca.query(Integer.parseInt(id));
		request.setAttribute("deltails", caselist);
		return "success";
	}
	public String delete(){
		ca.delete(Integer.parseInt(id));
		return "success";
	}
	
	public String update() throws IOException{
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		Caselist t = ca.query(Integer.parseInt(id));
		t.setId(t.getId());
		t.setTitle(title);
		
		String imgurl = upload();
		if(imgurl.equals("fail")){
			return "fail";
		}
		t.setImg(imgurl);
		t.setContent(content);
		t.setTime(time);
		t.setPublish(0);
		String str = ca.update(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";			
	}
	
	public String publish(){
		Caselist t = ca.query(Integer.parseInt(id));
		
		t.setId(t.getId());
		t.setTitle(t.getTitle());
		t.setContent(t.getContent());
		t.setTime(time);
		t.setPublish(1);
		String str = ca.update(t);
		if(("success").equals(str)){
			return "success";
		}
		return "fail";	
		
		
		
	}
}
